/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.domain.Group;
import me.zhengjie.modules.system.mapper.DictDetailMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.GroupService;
import me.zhengjie.modules.system.domain.vo.GroupQueryCriteria;
import me.zhengjie.modules.system.mapper.GroupMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author pysduck
* @date 2023-12-12
**/
@Service
@RequiredArgsConstructor
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {

    private final GroupMapper groupMapper;
    private final DictDetailMapper dictDetailMapper;

    @Override
    public PageResult<Group> queryAll(GroupQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(groupMapper.findAll(criteria, page));
    }

    @Override
    public List<Group> queryAll(GroupQueryCriteria criteria){
        return groupMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Group resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Group resources) {
        Group group = getById(resources.getId());
        group.copy(resources);
        saveOrUpdate(group);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Group> all, HttpServletResponse response) throws IOException {
        // 创建一个空的列表用于存储数据
        List<Map<String, Object>> list = new ArrayList<>();

        // 通过mapper查询班组类型信息，获取到列表
        List<DictDetail> groupType = dictDetailMapper.findByDictName("group_type");

        // 创建一个映射关系的map，用于存储班组类型和其对应的标签
        Map<String, String> groupTypeMap = new HashMap<>();

        // 遍历groupType列表，将班组类型和其对应的标签存储到groupTypeMap中
        groupType.forEach(o -> {
            groupTypeMap.put(o.getValue(), o.getLabel());
        });

        // 遍历all列表，每个group都表示一个班组信息
        for (Group group : all) {
            // 创建一个映射关系的map，用于存储班组信息
            Map<String, Object> map = new LinkedHashMap<>();

            // 将车间信息添加到map中
            map.put("车间", group.getFacility());

            // 将车间名称信息添加到map中
            map.put("车间名称", group.getFacilities().getFacilityname());

            // 将组织信息添加到map中
            map.put("组织", group.getOrganization());

            // 将班组类型信息添加到map中，通过groupTypeMap获取班组类型的标签
            map.put("班组类型", groupTypeMap.get(group.getGroupType()));

            // 将班组信息添加到map中
            map.put("班组", group.getTeamGroup());

            // 将班组名称信息添加到map中
            map.put("班组名称", group.getGroupName());

            // 将配置人数信息添加到map中
            map.put("配置人数", group.getConfiguredNumber());

            // 将默认班次信息添加到map中
            map.put("默认班次", group.getShifts().getShiftName());

            // 将备注信息添加到map中
            map.put("备注", group.getRemarks());

            // 将状态信息添加到map中，如果为禁用则存储为"禁用"，否则存储为"启用"
            map.put("状态", group.getStatus() == 0 ? "禁用" : "启用");

            // 将map添加到list中
            list.add(map);
        }

        // 调用FileUtil工具类的downloadExcel方法，将list中的数据下载为Excel文件，并通过response返回给前端
        FileUtil.downloadExcel(list, response);
    }

}